using System;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Resources;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Formatters.Binary;


namespace SypConst.Garbage
{

    [Serializable]
    public class SypGarbageObject : ICloneable 
    {
        protected SetText OutputMethod = null;

        protected string cleanArg(string arg)
        {
            Regex remover = new Regex(@"^['""]?(.*?)['""]?$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            arg = remover.Replace(arg, "$1");
            return arg;
        }

        public SypGarbageObject(SetText OutputMethod)
        {
            this.OutputMethod = OutputMethod;   
        }

        public object Clone()
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, this);
            ms.Position = 0;
            object obj = bf.Deserialize(ms);
            ms.Close();
            return obj;
        }

        public SypGarbageObject()
        {
        }

        public bool NetworkAvailable()
        {
            return System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
        }

        public string GetPrefix(string TplName)
        {
            string[] Token = TplName.Split(new char[] { '.' });
            return Token[0];
        }

        public bool CheckExtension(string filename, string extension)
        {
            string[] tab = filename.Split(new char[] { '.' });
            if (tab.Length > 2)
            {
                if (tab[1].Equals(extension))
                    return true;
            }
            return false;
        }

        protected string GetRessources(string key)
        {
            ResourceManager resourceManager = new ResourceManager("Syp.Resources", GetType().Assembly);

            ResourceSet CurrentResource = resourceManager.GetResourceSet(new CultureInfo(SypGarbageConf.Instance().Language), true, false);
            if (CurrentResource != null)
            {
                string val = CurrentResource.GetString(key);
                SypGarbage.free(CurrentResource);
                SypGarbage.free(resourceManager);
                return val;
            }
            SypGarbage.free(CurrentResource);
            SypGarbage.free(resourceManager);
            return null;
        }

    }
}
